package cn.juque.systemservice.service.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.juque.common.annotation.HandlerAppException;
import cn.juque.common.base.BaseResponseDTO;
import cn.juque.common.base.PageInfo;
import cn.juque.common.constants.MessageEnum;
import cn.juque.common.thread.BaseOperatorThreadLocal;
import cn.juque.common.utils.ValidatorUtil;
import cn.juque.systemservice.api.dto.menu.*;
import cn.juque.systemservice.api.provider.IMenuInfoProvider;
import cn.juque.systemservice.domain.MenuInfoDO;
import cn.juque.systemservice.entity.MenuInfo;
import cn.juque.systemservice.service.IMenuInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author JUQUE
 * @version 1.0.0
 * <ul>
 *  <li>simple-generator</li>
 *  <li>菜单</li>
 * </ul>
 * @date 2023-01-06 15:09:48
 **/
@Slf4j
@Service("menuInfoProvider")
@HandlerAppException
public class MenuInfoProviderImpl implements IMenuInfoProvider {

    @Resource
    private BaseOperatorThreadLocal baseOperatorThreadLocal;

    @Resource
    private IMenuInfoService menuInfoService;

    @Resource
    private ValidatorUtil validatorUtil;

    /**
     * 列表检索
     *
     * @param searchDTO 入参
     * @return List
     */
    @Override
    public BaseResponseDTO<List<MenuInfoListDTO>> list(MenuInfoSearchDTO searchDTO) {
        this.validatorUtil.checkNotNull(searchDTO);
        PageInfo pageInfo = searchDTO.getPageInfo();
        MenuInfoDO menuInfo = new MenuInfoDO();
        BeanUtil.copyProperties(searchDTO, menuInfo);
        List<MenuInfoDO> infoList = this.menuInfoService.listInfoWithoutPage(menuInfo);
        List<MenuInfoListDTO> infoDtoList = BeanUtil.copyToList(infoList, MenuInfoListDTO.class);
        return new BaseResponseDTO<>(MessageEnum.OK, infoDtoList, pageInfo);
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return MenuInfoDetailDTO
     */
    @Override
    public BaseResponseDTO<MenuInfoDetailDTO> detail(String id) {
        MenuInfo menuInfo = this.menuInfoService.detail(id);
        MenuInfoDetailDTO detailDTO = new MenuInfoDetailDTO();
        BeanUtil.copyProperties(menuInfo, detailDTO);
        return new BaseResponseDTO<>(MessageEnum.OK, detailDTO);
    }

    /**
     * 新增
     *
     * @param addDTO 入参
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> add(MenuInfoAddDTO addDTO) {
        this.validatorUtil.fastValidate(addDTO);
        this.baseOperatorThreadLocal.set(addDTO.getBaseOperator());
        MenuInfo menuInfo = new MenuInfo();
        BeanUtil.copyProperties(addDTO, menuInfo);
        this.menuInfoService.saveInfo(menuInfo);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }

    /**
     * 更新
     *
     * @param updateDTO 入参
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> update(MenuInfoUpdateDTO updateDTO) {
        this.validatorUtil.fastValidate(updateDTO);
        this.baseOperatorThreadLocal.set(updateDTO.getBaseOperator());
        MenuInfo menuInfo = new MenuInfo();
        BeanUtil.copyProperties(updateDTO, menuInfo);
        this.menuInfoService.updateInfo(menuInfo);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }

    /**
     * 删除
     *
     * @param req 入参
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> delete(MenuInfoDeleteDTO req) {
        List<String> infoIdList = req.getIdList();
        this.menuInfoService.deleteInfo(infoIdList);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }
}
